using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Model.Core;
using Model.Core.Entity;
using Model.Core.Entity.Dbcontexts;
using RobotWebApiService.Models;
using Swashbuckle.AspNetCore.Annotations;

namespace RobotWebApiService.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ActionNodeController : ControllerBase
    {
        private readonly DbcontextBase _context;
        private readonly ILogger<ActionNodeController> _logger;

        public ActionNodeController(DbcontextBase context, ILogger<ActionNodeController> logger)
        {
            _context = context;
            _logger = logger;
        }

        [HttpGet]
        [SwaggerOperation("获取所有ActionNode记录")]
        public async Task<ApiResponse<IEnumerable<ActionNode>>> GetAsync()
        {
            try
            {
                return new ApiResponse<IEnumerable<ActionNode>>(true, "", await _context.ActionNode.ToListAsync());
            }
            catch (Exception ex)
        {
            _logger.LogError(ex, "ActionNode控制器操作异常");
                return new ApiResponse<IEnumerable<ActionNode>>(false, ex.Message);
            }
        }

        [HttpPost]
        public async Task<ApiResponse<ActionNode>> PostAsync([FromBody] ActionNode entity)
        {
            try
            {
                if (!ModelState.IsValid)
                    return new ApiResponse<ActionNode>(false, "无效的请求数据");

                _context.ActionNode.Add(entity);
                await _context.SaveChangesAsync();
                return new ApiResponse<ActionNode>(true, "创建成功", entity);
            }
            catch (Exception ex)
        {
            _logger.LogError(ex, "ActionNode控制器操作异常");
                return new ApiResponse<ActionNode>(false, ex.Message);
            }
        }

        [HttpPut("{id}")]
        public async Task<ApiResponse<ActionNode>> PutAsync(string id, [FromBody] ActionNode entity)
        {
            try
            {
                if (Guid.Parse(id) != entity.Id)
                    return new ApiResponse<ActionNode>(false, "ID不匹配");

                _context.Entry(entity).State = EntityState.Modified;
                await _context.SaveChangesAsync();
                return new ApiResponse<ActionNode>(true, "更新成功", entity);
            }
            catch (Exception ex)
        {
            _logger.LogError(ex, "ActionNode控制器操作异常");
                return new ApiResponse<ActionNode>(false, ex.Message);
            }
        }

        [HttpDelete("{id}")]
        public async Task<ApiResponse<ActionNode>> DeleteAsync(string id)
        {
            try
            {
                var entity = _context.ActionNode.Find(id);
                if (entity == null)
                    return new ApiResponse<ActionNode>(false, "未找到该记录");

                _context.ActionNode.Remove(entity);
                await _context.SaveChangesAsync();
                return new ApiResponse<ActionNode>(true, "删除成功");
            }
            catch (Exception ex)
        {
            _logger.LogError(ex, "ActionNode控制器操作异常");
                return new ApiResponse<ActionNode>(false, ex.Message);
            }
        }
    }
}